;
(function () {
	hlSelectUi()
	new Vue({
		el: '#following',
		data: {
			data: '',
			isUnfold: true,
			channelType: [],
			id: hlUrlUtil.GetQueryString('id'), //项目id
			from: hlUrlUtil.GetQueryString('from'), //项目id
			followup_time: '', //跟进时间
			followup_time_H: '',
			followup_time_M: '',
			followup_type_code: '', //跟进方式
			followup_memo: '', //跟进记录
			plan_followup_time: '', //计划跟进时间
			plan_followup_time_H: '',
			plan_followup_time_M: '',
			plan_followup_content: '',


			channelCateArr: [],
			channelNameArr: [],
			processManagementArr: [],
			provinceList: [],
			cityList: [],
			areaList: [],
			companyArr: [],
			customizeArr: [],
			flag: false,
			showTable: true,
			detailFlag: true,
			channelCateCode: '#',
			channelNameCode: '#',
			processManagementCode: '#',
			projectName: '',
			projectNature: '',
			areaCoveredAcreage: '',
			buildingCoveredAcreage: '',
			floorCount: '',
			roomRates: '',
			standardStoreyHeight: '',
			liftCount: '',
			parkingCount: '',
			airConditionerCount: '',
			rentPrice: '',
			propertyFee: '',
			tradeZoneAvgPrice: '',
			businessForm: '',
			contactName: '',
			contactPhoneNum: '',
			buildingCompletionDate: '',
			proprietorName: '',
			proprietorProperty: '',
			developers: '',
			positionInfo: '',
			transportationFacilities: '',
			provinceId: "", //        省ID
			cityId: "", //        城市ID
			areaId: "", //        区ID
			provinceVal: '',
			cityVal: '',
			areaVal: '',
			address: '',
			projDesc: '',
			urlArr: [],
			projLegalName: '',
			companyFullName: '',
			legalPersonName: '',
			projPrOwnerName: '',
			compayId: '',
			rentAcreage: '',
			landUseRightsExpireDate: '',
			propertyAccessionDate: '',
			rightsInterestsRates: '',
			propertyProfile: '',
			status:'',
			assigneeId:'',
		},

		methods: {
			//选择跟进方式
			selectFollowType(code) {
				this.followup_type_code = code
			},
			// 获取跟进方式
			followType() {
				hlhttp.get({
					url: 'configure/dict',
					data: {
						module_code: 'SCTZ',
						config_name: 'SCTZ_FOLLOWUP_TYPE',
						parent_dict_code: '#'
					},
					success: (data) => {
						this.channelType = data;
					}
				})
			},
			//获取渠道分类
			channelCateDt() {
				hlhttp.get({
					url: 'configure/dict',
					data: {
						module_code: 'SCTZ',
						config_name: 'SCTZ_CHANNEL_CATE',
						parent_dict_code: '#'
					},
					success: (data) => {
						this.channelCateArr = data;
					}
				})
			},
			//获取渠道名称
			channelNameDt() {
				hlhttp.get({
					url: 'configure/dict',
					data: {
						module_code: 'SCTZ',
						config_name: 'SCTZ_CHANNEL_NAME',
						parent_dict_code: this.channelCateCode
					},
					success: (data) => {
						this.channelNameArr = data;
					}
				})
			},
			//选择渠道分类
			selectedCC(item) {
				this.channelCateCode = item.dictCode;
				this.channelNameArr = [];
				this.$refs.channelName.innerHTML = '请选择'
				this.channelNameDt()
			},
			//选择渠道名称
			selectedCN(item) {
				this.channelNameCode = item.dictCode;
			},
			//获取过程管理
			processManagementDt() {
				hlhttp.get({
					url: 'configure/dict',
					data: {
						module_code: 'SCTZ',
						config_name: 'SCTZ_PROCESS_MANAGER'
					},
					success: (data) => {
						this.processManagementArr = data;
					}
				})
			},
			//选择过程管理
			selectedPM(item) {
				this.processManagementCode = item.dictCode;
			},
			//省
			provinceData() {
				hlhttp.get({
					url: 'sub_regions',
					data: {
						region_id: '1'
					},
					success: (data) => {
						this.provinceList = data;
					}
				})
			},
			//设置省
			setProvince(data) {
				this.provinceId = data.regionId;
				this.provinceVal = data.name;
				this.cityData()
				this.areaList = [];
				this.$refs.citySel.innerHTML = '请选择城市';
				this.$refs.areaSel.innerHTML = '请选择区县';
			},
			//市
			cityData() {
				hlhttp.get({
					url: 'sub_regions',
					data: {
						region_id: this.provinceId
					},
					success: (data) => {
						this.cityList = data;
					}
				})
			},
			setCity(data) {
				this.cityId = data.regionId;
				this.cityVal = data.name;
				this.areaData()
				this.$refs.areaSel.innerHTML = '请选择区县';
			},
			//区
			areaData() {
				hlhttp.get({
					url: 'sub_regions',
					data: {
						region_id: this.cityId
					},
					success: (data) => {
						this.areaList = data;
					}
				})
			},
			setArea(data) {
				this.areaId = data.regionId;
				this.areaVal = data.name;
			},
			//上传图片
			upImg() {
				document.querySelector('.upImg').click()
			},
			//图片file
			imgFile() {
				imgupload0('.upImg', (data) => {
					this.urlArr.push({
						name: data.json.name,
						url: data.json.url,
						image_id: '#'
					})
				})
			},
			//删除图片
			delImg(i) {
				this.urlArr.splice(i, 1)
			},
			//获取公司
			companyDt() {
				hlhttp.get({
					url: 'companys',
					success: (data) => {
						this.companyArr = data;
					}
				})
			},
			//选择公司
			selectCompany(item) {
				this.companyId = item.compayId
			},
			//自定义字段radio选择
			radioSelected(i, code) {
				this.customizeArr[i].select = code
			},
			//自定义字段checkbox选择
			checkboxSelected(i, code) {
				if (this.customizeArr[i].select.indexOf(code) == -1) {
					this.customizeArr[i].select.push(code)
					return
				}
				this.customizeArr[i].select.splice(this.customizeArr[i].select.indexOf(code), 1)
			},
			//验证必填
			validata() {
				if (!this.channelCateCode) {
					hl.toast('请选择渠道分类')
					return false;
				}
				if (!this.projectName) {
					hl.toast('请填写项目名称')
					return false;
				}
				if (!this.contactName) {
					hl.toast('请填写联系人')
					return false;
				}
				if (!this.contactPhoneNum) {
					hl.toast('请填写联系电话')
					return false;
				}
				if (!this.provinceId) {
					hl.toast('请选择省份')
					return false;
				}
				if (!this.cityId) {
					hl.toast('请选择城市')
					return false;
				}
				if (!this.areaId) {
					hl.toast('请选择区县')
					return false;
				}
				return true;
			},
			//自定义字段获取
			marketCustomizeDt (val) {
				var that = this
				hlhttp.get({
					url: 'market/customize_column',
					success: (res) => {
						let arr = [...res.customizeColumns, ...res.systemColumns]
            for (var i = arr.length - 1; i >= 0; i--) {
              if (arr[i].isValid == 'false') {
                arr.splice(i,1)
              }
						}//去除多余数据（未启动，无关联）
						// if(val){
            //   arr = hlArrayUtil.removeRepeat([...val,...arr], 'configCode');
            // }

             //若有合同详情，则合并,去重（保留合同）
            let textList = []
            let noTextList = []
            arr.map((item,index) =>{
              if(item.fieldType == 'text'){
                textList.push(item)
              }else{
                noTextList.push(item)
              }
            })
            arr = [...noTextList,...textList]
            //重新排序，将文本类型放到最后
            arr.map(item => {
              if(!item.select){
                if (item.fieldType == 'radio' || item.fieldType == 'checkbox' || item.fieldType == 'select') {
                  item.select = [];
                  item.selectArr = item.fieldRange.split(',')
                  item.selectArrCopy = []
                  item.selectArr.map(item0 => {
                    item.selectArrCopy.push({
                      name: item0,
                      code: item0
                    })
                  })

                } else {
                  item.select = '';
                }
              }
						})
						if(val){
              val.map(item =>{
                arr.map(single =>{
                  if(single.configCode == item.configCode){
                    single.select = item.select
                  }
                })
              })
            }
          that.customizeArr = arr
					}
				})
			},
			// 检测自定义字段填写是否符合要求
      checkCustom() {
        let flag = true
        this.customizeArr.some(item => {
          if (item.isRequired == 'true' && !item.select.length) {
            flag = false
          }
        })
        return flag
      },
			// 保存
			submit() {
				if (!this.validata()) return;
				if(!this.checkCustom()){
          hl.toast('请完成自定义字段')
          return
        }
				let data = {
					status:this.status,
					assignee_id:this.assigneeId,
					channel_category_code: this.channelCateCode || '#',
					channel_name_code: this.channelNameCode || '#',
					process_manager_code: this.processManagementCode || '#',
					proj_name: this.projectName || '#',
					proj_property: this.projectNature || '#',
					area_covered_acreage: this.areaCoveredAcreage? (this.areaCoveredAcreage + ''):'#',
					building_covered_acreage: this.buildingCoveredAcreage? (this.buildingCoveredAcreage + ''):'#',
					floor_count: this.floorCount? (this.floorCount + ''):'#',
					room_rates: this.roomRates? (this.roomRates + ''):'#',
					standard_storey_height: this.standardStoreyHeight? (this.standardStoreyHeight + ''):'#' ,
					lift_count: this.liftCount? (this.liftCount + ''):'#' ,
					parking_count: this.parkingCount? (this.parkingCount + ''):'#',
					air_conditioner_count:this.airConditionerCount? (this.airConditionerCount + ''):'#',
					rent_price: this.rentPrice? (this.rentPrice + ''):'#' ,
					property_fee: this.propertyFee? (this.propertyFee + ''):'#' ,
					trade_zone_avg_price:  this.tradeZoneAvgPrice? (this.tradeZoneAvgPrice + ''):'#',
					business_form: this.businessForm? (this.businessForm + ''):'#',
					contact_name: this.contactName || '#',
					contact_phone_num: this.contactPhoneNum || '#',
					building_completion_date: this.buildingCompletionDate || '#',
					proprietor_name: this.proprietorName || '#',
					proprietor_property: this.proprietorProperty || '#',
					developers: this.developers || '#',
					position_info: this.positionInfo || '#',
					transportation_facilities: this.transportationFacilities || '#',
					province_id: this.provinceId || '#',
					city_id: this.cityId || '#',
					district_id: this.areaId || '#',
					province_name: this.provinceVal || '#',
					city_name: this.cityVal || '#',
					district_name: this.areaVal || '#',
					address: this.address || '#',
					address: this.address || '#',
					proj_desc: this.projDesc || '#',
					proj_legal_name: this.projLegalName || '#',
					company_full_name: this.companyFullName || '#',
					legal_person_name: this.legalPersonName || '#',
					proj_pr_owner_name: this.projPrOwnerName || '#',
					company_id: this.companyId || '#',
					company_name: this.$refs.company.innerHTML == '请选择' ? '#' : this.$refs.company.innerHTML,
					rent_acreage: this.rentAcreage? (this.rentAcreage + ''):'#',
					land_use_rights_expire_date: this.landUseRightsExpireDate || '#',
					property_accession_date: this.propertyAccessionDate || '#',
					rights_interests_rates: this.rightsInterestsRates ? this.rightsInterestsRates + '' : '#',
					property_profile: this.propertyProfile || '#',
				}
				var that = this
				hlhttp.post({
					url: 'market/Expansion_proj_followup_records	',
					data: {
						ref_id: this.id,
						followup_time: this.followup_time + ' ' + this.followup_time_H + ':' + this.followup_time_M +':00',
						followup_type_code: this.followup_type_code,
						followup_memo: this.followup_memo || '#',
						plan_followup_time: this.plan_followup_time + ' ' + this.plan_followup_time_H + ':' + this.plan_followup_time_M +':00',
						plan_followup_content: this.plan_followup_content || '#',
						custom_config_property:JSON.stringify(this.customizeArr),
						expansion_proj_info: JSON.stringify(data)
					},
					success: (res) => {
						hlUrlUtil.goTo('projectDetail',{'id': this.id, ddtab:true,first:'JL','from':this.from},false)
					}
				})
			},
			//验证项目是否重复
			validate() {
				if(!this.followup_time){
					hl.toast('请选择跟进日期')
					return
				}
				if(!this.followup_time_H || !this.followup_time_M){
					hl.toast('请输入跟进时间')
					return
				}
				if(!this.followup_type_code){
					hl.toast('请选择跟进方式')
					return
				}
				this.submit()
			},
			//获取详情
			detailDt() {
				hlhttp.get({
					url: 'market/expansion_proj_info',
					data: {
						expansion_proj_id: this.id
					},
					success: (data) => {
						this.assigneeId = data.expansionProjInfo.assigneeId || '#'
						this.status = data.expansionProjInfo.status
						this.channelCateCode = data.expansionProjInfo.channelCategoryCode;
						this.channelNameCode = data.expansionProjInfo.channelNameCode;
						this.processManagementCode = data.expansionProjInfo.processManagerCode;
						this.$refs.channelCate.innerHTML = data.expansionProjInfo.channelCategoryCode ? data.expansionProjInfo.channelCategoryCodeName : '请选择';
						this.$refs.channelName.innerHTML = data.expansionProjInfo.channelNameCode ? data.expansionProjInfo.channelNameCodeName : '请选择';
						this.$refs.processManagement.innerHTML = data.expansionProjInfo.processManagerCode ? data.expansionProjInfo.processManagerCodeName : '请选择';
						this.projectName = data.expansionProjInfo.projName;
						this.projectNature = data.expansionProjInfo.projProperty;
						this.areaCoveredAcreage = data.expansionProjInfo.areaCoveredAcreage;
						this.buildingCoveredAcreage = data.expansionProjInfo.buildingCoveredAcreage;
						this.floorCount = data.expansionProjInfo.floorCount;
						this.roomRates = data.expansionProjInfo.roomRates;
						this.standardStoreyHeight = data.expansionProjInfo.standardStoreyHeight;
						this.liftCount = data.expansionProjInfo.liftCount;
						this.parkingCount = data.expansionProjInfo.parkingCount;
						this.airConditionerCount = data.expansionProjInfo.airConditionerCount;
						this.rentPrice = data.expansionProjInfo.rentPrice;
						this.propertyFee = data.expansionProjInfo.propertyFee;
						this.tradeZoneAvgPrice = data.expansionProjInfo.tradeZoneAvgPrice;
						this.businessForm = data.expansionProjInfo.businessForm;
						this.contactName = data.expansionProjInfo.contactName;
						this.contactPhoneNum = data.expansionProjInfo.contactPhoneNum;
						this.buildingCompletionDate = hlDateUtil.formatUTCDate(data.expansionProjInfo.buildingCompletionDate, 'yyyy-MM-dd');
						this.proprietorName = data.expansionProjInfo.proprietorName;
						this.proprietorProperty = data.expansionProjInfo.proprietorProperty;
						this.developers = data.expansionProjInfo.developers;
						this.positionInfo = data.expansionProjInfo.positionInfo;
						this.transportationFacilities = data.expansionProjInfo.transportationFacilities;
						//省份区
						this.provinceId = data.expansionProjInfo.provinceId;
						this.cityId = data.expansionProjInfo.cityId;
						this.areaId = data.expansionProjInfo.districtId;
						this.$refs.provinceSel.innerHTML = data.expansionProjInfo.provinceName ? data.expansionProjInfo.provinceName : '请选择省份';
						this.$refs.citySel.innerHTML = data.expansionProjInfo.cityName ? data.expansionProjInfo.cityName : '请选择城市';
						this.$refs.areaSel.innerHTML = data.expansionProjInfo.districtName ? data.expansionProjInfo.districtName : '请选择区县';
						this.provinceVal  = data.expansionProjInfo.provinceName;
						this.cityVal = data.expansionProjInfo.cityName;
						this.areaVal = data.expansionProjInfo.districtName;
						//end
						this.address = data.expansionProjInfo.address;
						this.projDesc = data.expansionProjInfo.projDesc;
						this.projLegalName = data.expansionProjInfo.projLegalName;
						this.companyFullName = data.expansionProjInfo.companyFullName;
						this.legalPersonName = data.expansionProjInfo.legalPersonName;
						this.projPrOwnerName = data.expansionProjInfo.projPrOwnerName;
						this.rentAcreage = data.expansionProjInfo.rentAcreage;
						this.landUseRightsExpireDate = hlDateUtil.formatUTCDate(data.expansionProjInfo.landUseRightsExpireDate, 'yyyy-MM-dd');
						this.propertyAccessionDate = hlDateUtil.formatUTCDate(data.expansionProjInfo.propertyAccessionDate, 'yyyy-MM-dd');
						this.rightsInterestsRates = data.expansionProjInfo.rightsInterestsRates;
						this.propertyProfile = data.expansionProjInfo.propertyProfile;
						//公司
						this.companyId = data.expansionProjInfo.companyId;
						this.$refs.company.innerHTML = data.expansionProjInfo.companyId ? data.expansionProjInfo.companyName : '请选择';
						//img
						data.imagesUrl.map(item => {
							this.urlArr.push({
								name: '#',
								image_id: item.imageId,
								url: item.url
							})
						})
						//自定义
						let dataArr = data.customConfigProperty ? JSON.parse(data.customConfigProperty) : [];
						this.marketCustomizeDt(dataArr)
					}
				})
			}
		},
		mounted: function () {
			hlSelectUi()
			this.channelCateDt()
			this.processManagementDt()
			this.provinceData()
			this.companyDt()
			this.detailDt()
			this.followType()
		}
	})
}())
